package com.pn.controller;

import com.pn.dto.AssignAuthDto;
import com.pn.entity.Result;
import com.pn.entity.Role;
import com.pn.page.Page;
import com.pn.service.AuthService;
import com.pn.service.RoleService;
import com.pn.utils.CurrentUser;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private AuthService authService;

    /**
     * @return 查询所有角色的url接口role/role-list
     */
    @RequestMapping("role-list")
    public Result queryAllRole() {
        List<Role> roleList = roleService.getAllRole();
        return Result.ok(roleList);
    }

    /**
     * 查询用户已分配的角色的 url 接口/user/user-role-list/{userId}
     */
    @RequestMapping("/user-role-list/{userId}")
    public Result userRoleList(@PathVariable Integer userId) {
//执行业务
        List<Role> roleList = roleService.queryRolesByUserId(userId);
//响应
        return Result.ok(roleList);
    }

    /**
     * 分页查询角色的 url 接口/role/role-page-list
     * *
     * 参数 Page 对象用于接收请求参数页码 pageNum、 每页行数 pageSize;
     * 参数 Role 对象用于接收请求参数角色名 roleName、 角色代码 roleCode、 角色状态 roleState;
     * *
     * 返回值 Result 对象向客户端响应组装了所有分页信息的 Page 对象;
     */
    @RequestMapping("/role-page-list")
    public Result roleListPage(Page page, Role role) {
//执行业务
        page = roleService.queryRolePage(page, role);
//响应
        return Result.ok(page);
    }

    @RequestMapping("/role-add")
    public Result addRole(@RequestBody Role role, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String toke) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(toke);
        int createBy = currentUser.getUserId();
        role.setCreateBy(createBy);

        return roleService.saveRole(role);
    }

    /**
     * 修改角色状态的 url 接口/role/role-state-update
     *
     * @ RequestBody Role 将客户端传递的 json 数据封装到参数 Role 对象中;
     * @ RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token
     * 将请求头 Token 的值即客户端归还的 token 赋值给参数变量 token;
     */
    @RequestMapping("/role-state-update")
    public Result updateRoleState(@RequestBody Role role,
                                  @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户 id,即修改角色的用户 id
        int updateBy = currentUser.getUserId();
        //设置修改角色的用户 id 和修改时间
        role.setUpdateBy(updateBy);
        role.setUpdateTime(new Date());
        //执行业务
        Result result = roleService.updateRoleState(role);
        //响应
        return result;
    }

    @RequestMapping("/role-auth")
    public Result queryRoleAuth(Integer roleId) {
        List<Integer> authIdList = roleService.queryAuthIds(roleId);
        return Result.ok(authIdList);
    }

    /**
     * @param assignAuthDto 接收给角色分配权限(菜单)前端传递的数据的 Dto 类
     * @return 返回权限分配结果
     */
    @RequestMapping("/auth-grant")
    public Result assignAuth(@RequestBody AssignAuthDto assignAuthDto) {
        authService.assignAuth(assignAuthDto);
        return Result.ok("分配权限成功");
    }

//    删除角色的 url 接口/role/role-delete/{roleId}
    @RequestMapping("/role-delete/{roleId}")
    public Result deleteRole(@PathVariable Integer roleId) {
        roleService.deleteRole(roleId);
        return Result.ok("角色删除成功");
    }

    @RequestMapping("/role-update")
    public Result updateRole(@RequestBody Role role,@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int updateBy = currentUser.getUserId();
        role.setUpdateBy(updateBy);
        return roleService.updateRoleDesc(role);
    }
}
